<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Postback extends CI_Controller{    	
    function __construct(){
		parent::__construct();	
        parse_str(substr(strrchr($_SERVER['REQUEST_URI'], "?"), 1), $_GET);	
	}	
    function banner($idpb=0,$password=''){//postback với từng con off        
        if(!is_numeric($idpb)){$idpb=0;}
        $this->db->select('id,title,pb_value,rate');
        $network=$this->Home_model->get_one('network',array('idpb'=>$idpb,'pb_pass'=>$password));
        if(!empty($network)){//đúng pas và id pótback            
            $pb_value_array= unserialize($network->pb_value);//mang du lieu de get             
            $tracklink=$this->input->get_post($pb_value_array[0], TRUE); //->chinh la id_track
            if(!empty($tracklink)){
                //get du lieu ca 3 bang user offer va tracking
                $tracdt=$this->Home_model->get_one('tracklink',array('tracklink'=>$tracklink));
                $this->db->where('tracklink.tracklink',$tracklink);
                $this->db->select('
                                    tracklink.id,
                                    users.id as iduser,
                                    users.username,
                                    offer.title,
                                    offer.point_rate,                                                                   
                                    ');
                $this->db->from('tracklink');
                $this->db->join('users', 'users.id = tracklink.id_user','left'); 
                $this->db->join('offer', 'offer.id = tracklink.id_offer','left');   
                $tracdt = $this->db->get()->row();
                if(!empty($tracdt)){                    
                     $data = array(//add vao history
                           'iduser'=>$tracdt->iduser,
                           'point' => $tracdt->point_rate, 
                           'created'=>time(),
                           'type'=>1,//1 la net banner
                           'ip' =>$this->input->ip_address(),
                           'name' =>$network->title.'-'.$tracdt->title,
                           'idnet'=>$network->id                   
                        ); 
                     $this->addcredit($data);              
                     $this->thongbao($network->title); 
                }
            }                
                      
        }
    }
    function wall($idpb=0,$password=''){//postback với từng con off
        if(!is_numeric($idpb)){$idpb=0;}
        $this->db->select('id,title,pb_value,rate');
        $network=$this->Home_model->get_one('network',array('idpb'=>$idpb,'pb_pass'=>$password));
        if(!empty($network)){//đúng pas và id pótback   
               
            $pb_value_array= unserialize($network->pb_value);//mang du lieu de get 
            
            $iduser=$this->input->get_post($pb_value_array[0], TRUE); 
            $point=$this->input->get_post($pb_value_array[2], TRUE)*$network->rate;
            if(!empty($pb_value_array[4])){
                $name=$network->title.'-'.$this->input->get_post($pb_value_array[4], TRUE); 
            }
            if(empty($name)){
                $name=$network->title;
            }      
            $data = array(//add vao history
                   'iduser'=>$iduser,
                   'point' => $point, 
                   'created'=>time(),
                   'type'=>0,
                   'ip' =>$this->input->ip_address(),
                   'name' =>$name,
                   'idnet'=>$network->id                   
                );          
              $this->addcredit($data);              
              $this->thongbao($network->title);            
        }else{
            echo 0;
        }
    }
    function thongbao($netname=''){
        switch($netname){
            case 'Sonic':
                $eventId=$this->input->get('eventId');
                echo $eventId.":OK";
                break;
            case 'PaymentWall':
                echo 'OK';
            break;
            default:
                echo 1;
        }
        
    }
    function addcredit($data=''){      
            $this->db->insert('credit', $data);            
            $this->db->where('id', $data['iduser']);
            $this->db->set('curent', 'curent +'.$data['point'], FALSE);
            $this->db->set('total', 'total +'.$data['point'], FALSE);
            $this->db->update('users'); 
    }
    
    function debug(){
        if($_POST){$vv=serialize($_POST);}else $vv='';
        $uri = $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"].'--'.$vv;        
        //$this->db->insert('debug',array('debug1'=>$uri)); 
        $data = array(//add vao history
                   'iduser'=>$this->uri->segment(3),
                   'point' => $this->uri->segment(4), 
                   'created'=>time(),
                   'type'=>0,
                   'ip' =>$this->input->ip_address(),
                   'name' =>urldecode($this->uri->segment(5)),
                   'idnet'=>3                   
                );          
              $this->addcredit($data);       
        //echo $uri;
        if($_POST){
            //print_r($_POST);
        }
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */